package cn.chinaunicom.sdsi.gacx.author.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.chinaunicom.sdsi.frm.base.BaseService;
import cn.chinaunicom.sdsi.frm.sys.R;
import cn.chinaunicom.sdsi.frm.sys.entity.SysStaff;
import cn.chinaunicom.sdsi.gacx.author.dao.mybatis.AuthorMapper;
import cn.chinaunicom.sdsi.gacx.author.service.AuthorService;
import cn.chinaunicom.sdsi.gacx.web.service.impl.SelectBaseInfoServiceImpl;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 
 * @author chenxl 20170608
 *
 */
@Service
public class AuthorServiceImpl extends BaseService implements AuthorService {
	private final Log log = LogFactory.getLog(AuthorServiceImpl.class);
	@Autowired
	private AuthorMapper authorMapper;

	/**
	 * 获取本单位待备案数量
	 */
	@Override
	public JSONObject canUseSysCheck() {
		SysStaff staff = getSessionCache(R.UserSign.SIGNIN_USER_SESSION);
		String unitId = staff.getUnitId();

		JSONObject rObj = new JSONObject();
		boolean canUse = true;
		int count = authorMapper.getToDoBaTaskCount(unitId);
		if (count > 0) {
			canUse = false;
		}

		rObj.element("canUse", canUse);
		return rObj;
	}

	/*
	 * 根据公函、号码清单过滤可查询的号码
	 */
	@Override
	public JSONObject canSelectCheck(String elecId, List numList) {
		JSONObject rObj = new JSONObject();
		String returnFlag = "0000";
		String returnInfo = "成功";
		Object selectType = "";
		List canSelectList = new ArrayList<>();

		List rList = authorMapper.getCanSelectTaskByElecId(elecId);
		log.debug(">> 20170706 rList:" + rList.size());
		if (rList.size() > 0) {
			Map tmp = (Map) rList.get(0);
			selectType = (String)tmp.get("selectType");
			if (numList.size() > 0) {
				Map<String, Object> paramMap = new HashMap<String, Object>();
				paramMap.put("elecId", elecId);
				paramMap.put("numList", numList);
				log.debug(">> 20170707 numList:" + numList);
				rList = authorMapper.getCanSelectNumByElecId(paramMap);
				if (rList.size() > 0) {
					for (int i = 0; i < rList.size(); i++) {
						tmp = (Map) rList.get(i);
						canSelectList.add(tmp.get("num"));
					}
				} else {
					returnFlag = "1001";
					returnInfo = "未匹配到符合查询条件的号码或身份证";
				}
			}
		} else {
			returnFlag = "1000";
			returnInfo = "未查询到公函或公函已超过查询有效期";
		}
		rObj.element("returnFlag", returnFlag);
		rObj.element("returnInfo", returnInfo);
		rObj.element("selectType", selectType);
		rObj.element("numInfo", canSelectList);

		return rObj;
	}

}